package com.bolang.onlycode.demo.Cache;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Memorizer2<A,V>implements Computable<A,V> {
    // 得有个容器来放缓存
    private final Map<A,V> cache =new ConcurrentHashMap<A,V>();
    // 得有计算功能
    private final Computable<A,V> c;
    public Memorizer2(Computable<A,V> c){
        this.c=c;
    }
    @Override
    public V compute(A arg) throws InterruptedException {
        V result =cache.get(arg);
        if(result == null){
            result = c.compute(arg);
            cache.put(arg,result);
        }
        return result;
    }
}
